home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / command / sdist.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  15.1 KB  |  396 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.command.sdist
  5.  
  6. Implements the Distutils 'sdist' command (create a source distribution)."""
  7. __revision__ = '$Id: sdist.py 68968 2009-01-26 17:20:15Z tarek.ziade $'
  8. import os
  9. import string
  10. import sys
  11. from types import *
  12. from glob import glob
  13. from distutils.core import Command
  14. from distutils import dir_util, dep_util, file_util, archive_util
  15. from distutils.text_file import TextFile
  16. from distutils.errors import *
  17. from distutils.filelist import FileList
  18. from distutils import log
  19.  
  20. def show_formats():
  21.     '''Print all possible values for the \'formats\' option (used by
  22.     the "--help-formats" command-line option).
  23.     '''
  24.     FancyGetopt = FancyGetopt
  25.     import distutils.fancy_getopt
  26.     ARCHIVE_FORMATS = ARCHIVE_FORMATS
  27.     import distutils.archive_util
  28.     formats = []
  29.     for format in ARCHIVE_FORMATS.keys():
  30.         formats.append(('formats=' + format, None, ARCHIVE_FORMATS[format][2]))
  31.     
  32.     formats.sort()
  33.     pretty_printer = FancyGetopt(formats)
  34.     pretty_printer.print_help('List of available source distribution formats:')
  35.  
  36.  
  37. class sdist(Command):
  38.     description = 'create a source distribution (tarball, zip file, etc.)'
  39.     user_options = [
  40.         ('template=', 't', 'name of manifest template file [default: MANIFEST.in]'),
  41.         ('manifest=', 'm', 'name of manifest file [default: MANIFEST]'),
  42.         ('use-defaults', None, 'include the default file set in the manifest [default; disable with --no-defaults]'),
  43.         ('no-defaults', None, "don't include the default file set"),
  44.         ('prune', None, 'specifically exclude files/directories that should not be distributed (build tree, RCS/CVS dirs, etc.) [default; disable with --no-prune]'),
  45.         ('no-prune', None, "don't automatically exclude anything"),
  46.         ('manifest-only', 'o', 'just regenerate the manifest and then stop (implies --force-manifest)'),
  47.         ('force-manifest', 'f', 'forcibly regenerate the manifest and carry on as usual'),
  48.         ('formats=', None, 'formats for source distribution (comma-separated list)'),
  49.         ('keep-temp', 'k', 'keep the distribution tree around after creating ' + 'archive file(s)'),
  50.         ('dist-dir=', 'd', 'directory to put the source distribution archive(s) in [default: dist]')]
  51.     boolean_options = [
  52.         'use-defaults',
  53.         'prune',
  54.         'manifest-only',
  55.         'force-manifest',
  56.         'keep-temp']
  57.     help_options = [
  58.         ('help-formats', None, 'list available distribution formats', show_formats)]
  59.     negative_opt = {
  60.         'no-defaults': 'use-defaults',
  61.         'no-prune': 'prune' }
  62.     default_format = {
  63.         'posix': 'gztar',
  64.         'nt': 'zip' }
  65.     
  66.     def initialize_options(self):
  67.         self.template = None
  68.         self.manifest = None
  69.         self.use_defaults = 1
  70.         self.prune = 1
  71.         self.manifest_only = 0
  72.         self.force_manifest = 0
  73.         self.formats = None
  74.         self.keep_temp = 0
  75.         self.dist_dir = None
  76.         self.archive_files = None
  77.  
  78.     
  79.     def finalize_options(self):
  80.         if self.manifest is None:
  81.             self.manifest = 'MANIFEST'
  82.         
  83.         if self.template is None:
  84.             self.template = 'MANIFEST.in'
  85.         
  86.         self.ensure_string_list('formats')
  87.         if self.formats is None:
  88.             
  89.             try:
  90.                 self.formats = [
  91.                     self.default_format[os.name]]
  92.             except KeyError:
  93.                 raise DistutilsPlatformError, "don't know how to create source distributions " + 'on platform %s' % os.name
  94.             except:
  95.                 None<EXCEPTION MATCH>KeyError
  96.             
  97.  
  98.         None<EXCEPTION MATCH>KeyError
  99.         bad_format = archive_util.check_archive_formats(self.formats)
  100.         if bad_format:
  101.             raise DistutilsOptionError, "unknown archive format '%s'" % bad_format
  102.         bad_format
  103.         if self.dist_dir is None:
  104.             self.dist_dir = 'dist'
  105.         
  106.  
  107.     
  108.     def run(self):
  109.         self.filelist = FileList()
  110.         self.check_metadata()
  111.         self.get_file_list()
  112.         if self.manifest_only:
  113.             return None
  114.         self.make_distribution()
  115.  
  116.     
  117.     def check_metadata(self):
  118.         '''Ensure that all required elements of meta-data (name, version,
  119.         URL, (author and author_email) or (maintainer and
  120.         maintainer_email)) are supplied by the Distribution object; warn if
  121.         any are missing.
  122.         '''
  123.         metadata = self.distribution.metadata
  124.         missing = []
  125.         for attr in ('name', 'version', 'url'):
  126.             if not hasattr(metadata, attr) and getattr(metadata, attr):
  127.                 missing.append(attr)
  128.                 continue
  129.         
  130.         if missing:
  131.             self.warn('missing required meta-data: ' + string.join(missing, ', '))
  132.         
  133.         if metadata.author:
  134.             if not metadata.author_email:
  135.                 self.warn("missing meta-data: if 'author' supplied, " + "'author_email' must be supplied too")
  136.             
  137.         elif metadata.maintainer:
  138.             if not metadata.maintainer_email:
  139.                 self.warn("missing meta-data: if 'maintainer' supplied, " + "'maintainer_email' must be supplied too")
  140.             
  141.         else:
  142.             self.warn('missing meta-data: either (author and author_email) ' + 'or (maintainer and maintainer_email) ' + 'must be supplied')
  143.  
  144.     
  145.     def get_file_list(self):
  146.         """Figure out the list of files to include in the source
  147.         distribution, and put it in 'self.filelist'.  This might involve
  148.         reading the manifest template (and writing the manifest), or just
  149.         reading the manifest, or just using the default file set -- it all
  150.         depends on the user's options and the state of the filesystem.
  151.         """
  152.         template_exists = os.path.isfile(self.template)
  153.         if template_exists:
  154.             template_newer = dep_util.newer(self.template, self.manifest)
  155.         
  156.         self.debug_print('checking if %s newer than %s' % (self.distribution.script_name, self.manifest))
  157.         setup_newer = dep_util.newer(self.distribution.script_name, self.manifest)
  158.         if not template_exists and template_newer:
  159.             pass
  160.         manifest_outofdate = setup_newer
  161.         if not self.force_manifest:
  162.             pass
  163.         force_regen = self.manifest_only
  164.         manifest_exists = os.path.isfile(self.manifest)
  165.         if not template_exists:
  166.             pass
  167.         neither_exists = not manifest_exists
  168.         if manifest_outofdate and neither_exists or force_regen:
  169.             if not template_exists:
  170.                 self.warn(("manifest template '%s' does not exist " + '(using default file list)') % self.template)
  171.             
  172.             self.filelist.findall()
  173.             if self.use_defaults:
  174.                 self.add_defaults()
  175.             
  176.             if template_exists:
  177.                 self.read_template()
  178.             
  179.             if self.prune:
  180.                 self.prune_file_list()
  181.             
  182.             self.filelist.sort()
  183.             self.filelist.remove_duplicates()
  184.             self.write_manifest()
  185.         else:
  186.             self.read_manifest()
  187.  
  188.     
  189.     def add_defaults(self):
  190.         """Add all the default files to self.filelist:
  191.           - README or README.txt
  192.           - setup.py
  193.           - test/test*.py
  194.           - all pure Python modules mentioned in setup script
  195.           - all C sources listed as part of extensions or C libraries
  196.             in the setup script (doesn't catch C headers!)
  197.         Warns if (README or README.txt) or setup.py are missing; everything
  198.         else is optional.
  199.         """
  200.         standards = [
  201.             ('README', 'README.txt'),
  202.             self.distribution.script_name]
  203.         for fn in standards:
  204.             if type(fn) is TupleType:
  205.                 alts = fn
  206.                 got_it = 0
  207.                 for fn in alts:
  208.                     if os.path.exists(fn):
  209.                         got_it = 1
  210.                         self.filelist.append(fn)
  211.                         break
  212.                         continue
  213.                 
  214.                 if not got_it:
  215.                     self.warn('standard file not found: should have one of ' + string.join(alts, ', '))
  216.                 
  217.             got_it
  218.             if os.path.exists(fn):
  219.                 self.filelist.append(fn)
  220.                 continue
  221.             self.warn("standard file '%s' not found" % fn)
  222.         
  223.         optional = [
  224.             'test/test*.py',
  225.             'setup.cfg']
  226.         for pattern in optional:
  227.             files = filter(os.path.isfile, glob(pattern))
  228.             if files:
  229.                 self.filelist.extend(files)
  230.                 continue
  231.         
  232.         if self.distribution.has_pure_modules():
  233.             build_py = self.get_finalized_command('build_py')
  234.             self.filelist.extend(build_py.get_source_files())
  235.         
  236.         if self.distribution.has_ext_modules():
  237.             build_ext = self.get_finalized_command('build_ext')
  238.             self.filelist.extend(build_ext.get_source_files())
  239.         
  240.         if self.distribution.has_c_libraries():
  241.             build_clib = self.get_finalized_command('build_clib')
  242.             self.filelist.extend(build_clib.get_source_files())
  243.         
  244.         if self.distribution.has_scripts():
  245.             build_scripts = self.get_finalized_command('build_scripts')
  246.             self.filelist.extend(build_scripts.get_source_files())
  247.         
  248.  
  249.     
  250.     def read_template(self):
  251.         '''Read and parse manifest template file named by self.template.
  252.  
  253.         (usually "MANIFEST.in") The parsing and processing is done by
  254.         \'self.filelist\', which updates itself accordingly.
  255.         '''
  256.         log.info("reading manifest template '%s'", self.template)
  257.         template = TextFile(self.template, strip_comments = 1, skip_blanks = 1, join_lines = 1, lstrip_ws = 1, rstrip_ws = 1, collapse_join = 1)
  258.         while None:
  259.             line = template.readline()
  260.             if line is None:
  261.                 break
  262.             
  263.             
  264.             try:
  265.                 self.filelist.process_template_line(line)
  266.             continue
  267.             except DistutilsTemplateError:
  268.                 msg = None
  269.                 self.warn('%s, line %d: %s' % (template.filename, template.current_line, msg))
  270.                 continue
  271.             
  272.  
  273.             return None
  274.  
  275.     
  276.     def prune_file_list(self):
  277.         '''Prune off branches that might slip into the file list as created
  278.         by \'read_template()\', but really don\'t belong there:
  279.           * the build tree (typically "build")
  280.           * the release tree itself (only an issue if we ran "sdist"
  281.             previously with --keep-temp, or it aborted)
  282.           * any RCS, CVS, .svn, .hg, .git, .bzr, _darcs directories
  283.         '''
  284.         build = self.get_finalized_command('build')
  285.         base_dir = self.distribution.get_fullname()
  286.         self.filelist.exclude_pattern(None, prefix = build.build_base)
  287.         self.filelist.exclude_pattern(None, prefix = base_dir)
  288.         if sys.platform == 'win32':
  289.             seps = '/|\\\\'
  290.         else:
  291.             seps = '/'
  292.         vcs_dirs = [
  293.             'RCS',
  294.             'CVS',
  295.             '\\.svn',
  296.             '\\.hg',
  297.             '\\.git',
  298.             '\\.bzr',
  299.             '_darcs']
  300.         vcs_ptrn = '(^|%s)(%s)(%s).*' % (seps, '|'.join(vcs_dirs), seps)
  301.         self.filelist.exclude_pattern(vcs_ptrn, is_regex = 1)
  302.  
  303.     
  304.     def write_manifest(self):
  305.         """Write the file list in 'self.filelist' (presumably as filled in
  306.         by 'add_defaults()' and 'read_template()') to the manifest file
  307.         named by 'self.manifest'.
  308.         """
  309.         self.execute(file_util.write_file, (self.manifest, self.filelist.files), "writing manifest file '%s'" % self.manifest)
  310.  
  311.     
  312.     def read_manifest(self):
  313.         """Read the manifest file (named by 'self.manifest') and use it to
  314.         fill in 'self.filelist', the list of files to include in the source
  315.         distribution.
  316.         """
  317.         log.info("reading manifest file '%s'", self.manifest)
  318.         manifest = open(self.manifest)
  319.         while None:
  320.             line = manifest.readline()
  321.             if line == '':
  322.                 break
  323.             
  324.             if line[-1] == '\n':
  325.                 line = line[0:-1]
  326.             
  327.             continue
  328.             manifest.close()
  329.             return None
  330.  
  331.     
  332.     def make_release_tree(self, base_dir, files):
  333.         """Create the directory tree that will become the source
  334.         distribution archive.  All directories implied by the filenames in
  335.         'files' are created under 'base_dir', and then we hard link or copy
  336.         (if hard linking is unavailable) those files into place.
  337.         Essentially, this duplicates the developer's source tree, but in a
  338.         directory named after the distribution, containing only the files
  339.         to be distributed.
  340.         """
  341.         self.mkpath(base_dir)
  342.         dir_util.create_tree(base_dir, files, dry_run = self.dry_run)
  343.         if hasattr(os, 'link'):
  344.             link = 'hard'
  345.             msg = 'making hard links in %s...' % base_dir
  346.         else:
  347.             link = None
  348.             msg = 'copying files to %s...' % base_dir
  349.         if not files:
  350.             log.warn('no files to distribute -- empty manifest?')
  351.         else:
  352.             log.info(msg)
  353.         for file in files:
  354.             if not os.path.isfile(file):
  355.                 log.warn("'%s' not a regular file -- skipping" % file)
  356.                 continue
  357.             dest = os.path.join(base_dir, file)
  358.             self.copy_file(file, dest, link = link)
  359.         
  360.         self.distribution.metadata.write_pkg_info(base_dir)
  361.  
  362.     
  363.     def make_distribution(self):
  364.         """Create the source distribution(s).  First, we create the release
  365.         tree with 'make_release_tree()'; then, we create all required
  366.         archive files (according to 'self.formats') from the release tree.
  367.         Finally, we clean up by blowing away the release tree (unless
  368.         'self.keep_temp' is true).  The list of archive files created is
  369.         stored so it can be retrieved later by 'get_archive_files()'.
  370.         """
  371.         base_dir = self.distribution.get_fullname()
  372.         base_name = os.path.join(self.dist_dir, base_dir)
  373.         self.make_release_tree(base_dir, self.filelist.files)
  374.         archive_files = []
  375.         if 'tar' in self.formats:
  376.             self.formats.append(self.formats.pop(self.formats.index('tar')))
  377.         
  378.         for fmt in self.formats:
  379.             file = self.make_archive(base_name, fmt, base_dir = base_dir)
  380.             archive_files.append(file)
  381.             self.distribution.dist_files.append(('sdist', '', file))
  382.         
  383.         self.archive_files = archive_files
  384.         if not self.keep_temp:
  385.             dir_util.remove_tree(base_dir, dry_run = self.dry_run)
  386.         
  387.  
  388.     
  389.     def get_archive_files(self):
  390.         """Return the list of archive files created when the command
  391.         was run, or None if the command hasn't run yet.
  392.         """
  393.         return self.archive_files
  394.  
  395.  
  396.